package com.lidong.framework.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static final String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
	public static final String FORMAT_YMDHM = "yyyy-MM-dd HH:mm";
	public static final String FORMAT_YMDH = "yyyy-MM-dd HH";
	public static final String FORMAT_YMD = "yyyy-MM-dd";
	public static final String FORMAT_MDHM = "MM.dd HH:mm";
	
	/**
	 * 时间转换为字符串yyyy-MM-dd HH:mm:ss
	 * @param date 要转换的时间
	 * @return 转换后的字符串
	 */
	public static String formatYMDHMS(Date date) {
		if(date == null){
			return null;
		}
		return new SimpleDateFormat(FORMAT_YMDHMS).format(date);
	}
	/**
	 * 时间字符串转换Date
	 * @param date
	 * @return
	 */
	public static Date formatYMDHMS(String date) {
		if(date == null){
			return null;
		}
		try {
			return new SimpleDateFormat(FORMAT_YMDHMS).parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 时间转换为字符串yyyy-MM-dd
	 * @param date 要转换的时间
	 * @return 转换后的字符串
	 */
	public static String formatYMD(Date date) {
		if(date == null){
			return null;
		}
		return new SimpleDateFormat(FORMAT_YMD).format(date);
	}
	
	/**
	 * 时间字符转转换为Date
	 * @param date 要转换的时间
	 * @return 转换后的字符串
	 */
	public static Date formatYMD(String date) {
		if(date == null){
			return null;
		}
		try {
			return new SimpleDateFormat(FORMAT_YMD).parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 日期格式化为 月.日 时:分
	 * @param date
	 * @return
	 */
	public static String formatMDHM(Date date) {
		if(date == null){
			return null;
		}
		return new SimpleDateFormat(FORMAT_MDHM).format(date);
	}
	
	/**
	 * 将日期格式化成指定时间
	 * @param date 要格式化的时间
	 * @param format 例如yyyy-MM-dd
	 * @return	转换后的字符串
	 */
	public static final String format(Date date, String format) {
		if (date == null){
			return null;
		}
		return new SimpleDateFormat(format).format(date);
	}
	
	/**
	 * 将字符串格式化成指定时间
	 * @param date 要格式化的时间
	 * @param format 例如yyyy-MM-dd
	 * @return	转换后的字符串
	 */
	public static final Date format(String date, String format) {
		if (StringUtil.isBlank(date)){
			return null;
		}
		try {
			return new SimpleDateFormat(format).parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 在当前时间加减指定天
	 * @param nowDate 当前时间
	 * @param day 加减天数
	 * @return
	 */
	public static final Date getAfterDay(Date nowDate, int day){
		if (nowDate == null){
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(nowDate);
		cal.add(Calendar.DAY_OF_MONTH, day);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	/**
	 * 在当前时间加减指定天
	 * @param nowDate 当前时间
	 * @param day 加减天数
	 * @param resetTime	是否清空时分秒
	 * @return
	 */
	public static final Date getAfterDay(Date nowDate, int day, boolean resetTime){
		if (nowDate == null){
			return null;
		}
		Calendar cal = Calendar.getInstance();
		cal.setTime(nowDate);
		cal.add(Calendar.DAY_OF_MONTH, day);
		if(resetTime){
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			cal.set(Calendar.MILLISECOND, 0);
		}
		return cal.getTime();
	}
	
	public static final Date getToDay(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	
	/**
	 * 返回两个时间差(秒)
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static long getSecondDiff(Date d1, Date d2) {
		long d1t = d1.getTime();
		long d2t = d2.getTime();
		return ((d2t - d1t) / 1000);
	}
	
	/**
	 * 分钟的加减
	 * @param date
	 * @param minute
	 * @return
	 */
	public static Date getDateAfterMinute(Date date, int minute) {
		if (date == null){
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTime();
	}
	/**
	 * 小时的加减
	 * @param date
	 * @param hours
	 * @return
	 */
	public static Date getDateAfterHour(Date date, int hours) {
		if (date == null){
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.HOUR, hours);
		return calendar.getTime();
	}
	
	/**
	 * 时间加减天数
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date getDayAdd(Date date, int days) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, days);
		return new Date(cal.getTime().getTime());
	}
	
	public static int getTimeInt(){
		Calendar calendar = Calendar.getInstance();
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);
		return Integer.parseInt(hour + "" + minute);
	}
}
